package com.base.mine.fag;

import android.view.View;

import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.bean.User;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.RxBus2;
import com.base.mine.R;
import com.base.mine.databinding.FagModifysexBinding;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2020/3/26.
 */

public class ModifySexFragment extends CBaseMvpFragment{
    private FagModifysexBinding mBinding;
    protected CompositeDisposable mCompositeDisposable=new CompositeDisposable();
    @Override
    public void init() {
        mBinding = (FagModifysexBinding) mRootBinding;
    }

    @Override
    public void start() {

        mBinding.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                modifySex();
            }
        });
        if (BaseApp.getUser().getGender()!=null) {
            if (BaseApp.getUser().getGender() == 1) {
                mBinding.men.setChecked(true);
            } else {
                mBinding.women.setChecked(true);
            }
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fag_modifysex;
    }

    private void modifySex(){
        showPb_mes("正在保存数据");
        Map<String,Object> param = new HashMap<>();
        if (mBinding.men.isChecked()){
            param.put("gender",1);
        }else {
            param.put("gender",0);
        }
        Disposable enqueue = RealNetWorkUtil.get().method("api-auth/user/updateGender").params(param)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        hidePb_mes();
                        showMes(msg);
                    }

                    @Override
                    public void onNext(String o) {
                        hidePb_mes();
                        User user = BaseApp.getUser();
                        if (mBinding.men.isChecked()){
                            user.setGender(1);
                        }else {
                            user.setGender(0);
                        }
                        BaseApp.saveUser(user);
                        RxBus2.getInstance().post(BaseApp.getUser());
                        showMes("保存成功");
                        _mActivity.finish();
                    }
                });
        mCompositeDisposable.add(enqueue);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mCompositeDisposable.dispose();
    }
}
